<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <div class="header" :class="{ 'header-hidden': isHeaderHidden }">
      <div class="header-content">
        <div class="header-left">
          <a href="#" class="nav-link">下载app</a>
          <a href="#" class="nav-link">添加到桌面</a>
          <a href="#" class="nav-link">关于头条</a>
          <a href="#" class="nav-link">反馈</a>
          <a href="#" class="nav-link">侵权投诉</a>
        </div>
        <div class="header-right">
          <a href="#" class="nav-link">消息</a>
          <a href="#" class="nav-link">发布作品</a>
          <el-button type="danger" size="small">登录</el-button>
        </div>
      </div>
    </div>

    <!-- 搜索区域 -->
    <div class="search-section" :class="{ 'search-hidden': isHeaderHidden }">
      <div class="search-content">
        <img src="../assets/toutiao-logo.png" alt="课头条" class="logo">
        <div class="search-box">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索站内资讯"
            :prefix-icon="Search"
            class="search-input"
          >
            <template #append>
              <el-button type="primary">
                <el-icon><Search /></el-icon>
              </el-button>
            </template>
          </el-input>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-container">
      <!-- 左侧导航 -->
      <div class="nav-section" :class="{ 'nav-hidden': isHeaderHidden }">
        <div class="nav-content">
          <div class="nav-menu">
            <a
              v-for="(item, index) in navItems"
              :key="index"
              href="#"
              class="nav-item"
              :class="{ active: currentNav === index }"
              @click="currentNav = index"
            >
              {{ item }}
            </a>
          </div>
        </div>
      </div>

      <!-- 主内容区 -->
      <div class="main-content">
        <el-row :gutter="20">
          <!-- 左侧新闻列表 -->
          <el-col :span="16">
            <!-- 登录提示区 -->
            <div class="login-tip">
              <span class="tip-text">登录后，头条更懂你</span>
              <el-button type="danger" class="login-btn">立即登录</el-button>
            </div>

            <!-- 新闻列表 -->
            <div v-for="(item, index) in newsList" :key="index" class="news-item">
              <el-card shadow="hover">
                <div class="news-content">
                  <div class="news-text">
                    <h3>{{ item.title }}</h3>
                    <p class="news-info">
                      <span>{{ item.source }}</span>
                      <span>{{ item.comments }}评论</span>
                      <span>{{ item.time }}</span>
                    </p>
                  </div>
                  <el-image
                    v-if="item.image"
                    :src="item.image"
                    fit="cover"
                    class="news-image"
                  />
                </div>
              </el-card>
            </div>
          </el-col>

          <!-- 右侧热门榜单 -->
          <el-col :span="8">
            <el-card class="hot-list">
              <template #header>
                <div class="hot-list-header">
                  <span>热门榜单</span>
                  <el-button text>更多 ></el-button>
                </div>
              </template>
              <div v-for="(item, index) in hotList" :key="index" class="hot-item">
                <span class="hot-rank" :class="{ 'top3': index < 3 }">{{ index + 1 }}</span>
                <span class="hot-title">{{ item.title }}</span>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { Search } from '@element-plus/icons-vue'

const searchKeyword = ref('')
const isHeaderHidden = ref(false)
const lastScrollTop = ref(0)
const currentNav = ref(0)

// 导航菜单项
const navItems = [
  '推荐',
  '热点',
  '视频',
  '深圳',
  '科技',
  '娱乐',
  '游戏',
  '更多'
]

// 处理滚动事件
const handleScroll = () => {
  const currentScrollTop = window.pageYOffset || document.documentElement.scrollTop

  if (currentScrollTop > 200) {
    isHeaderHidden.value = currentScrollTop > lastScrollTop.value
  } else {
    isHeaderHidden.value = false
  }

  lastScrollTop.value = currentScrollTop
}

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})

// 新闻列表数据
const newsList = ref([
  {
    title: '最新科技创新成果发布',
    source: '科技日报',
    comments: 128,
    time: '1小时前',
    image: 'https://via.placeholder.com/200x150'
  },
  {
    title: '2024年全球经济展望分析',
    source: '经济观察报',
    comments: 256,
    time: '2小时前',
    image: 'https://via.placeholder.com/200x150'
  }
])

// 热门榜单数据
const hotList = ref([
  { title: '新能源汽车产业发展新动向' },
  { title: '人工智能技术最新突破' },
  { title: '国际体育赛事精彩回顾' },
  { title: '教育改革新政策解读' },
  { title: '环保技术创新应用案例' }
])
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  background-color: #f4f5f6;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background-color: #222;
  height: 34px;
  color: #fff;
  transition: transform 0.3s ease;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-hidden {
  transform: translateY(-100%);
}

.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-link {
  color: #fff;
  font-size: 13px;
  text-decoration: none;
  opacity: 0.8;
  transition: opacity 0.2s;
}

.nav-link:hover {
  opacity: 1;
}

/* 搜索区域样式 */
.search-section {
  position: fixed;
  top: 34px;
  left: 0;
  right: 0;
  z-index: 999;
  background-color: #fff;
  padding: 16px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.search-hidden {
  transform: translateY(-100%);
}

.search-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 30px;
  padding: 0 20px;
  position: relative;
}

.logo {
  height: 30px;
  position: absolute;
  left: 20px;
}

.search-box {
  width: 500px;
  margin: 0 auto;
}

.search-input :deep(.el-input__wrapper) {
  border-radius: 4px;
}

.search-input :deep(.el-input__inner) {
  height: 40px;
}

/* 主容器布局 */
.main-container {
  margin-top: 106px;  /* 为顶部固定元素留出空间 */
  display: flex;
  max-width: 1200px;
  margin: 120px auto 0;
  padding: 0 20px;
}

/* 左侧导航样式调整 */
.nav-section {
  position: sticky;
  top: 106px;
  width: 110px;
  height: fit-content;
  background: transparent;
  margin-right: 30px;
}

.nav-content {
  padding: 0;
}

.nav-menu {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.nav-item {
  font-size: 16px;
  color: #444;
  text-decoration: none;
  position: relative;
  transition: color 0.2s;
  padding: 5px 0;
}

/* .nav-item:hover {
  color: #f85959;
} */

.nav-item.active {
  color: #f85959;
  font-weight: bold;
}

.nav-item.active::after {
  content: '';
  position: absolute;
  left: -10px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 16px;
  background-color: #f85959;
  border-radius: 2px;
}

/* 主内容区样式调整 */
.main-content {
  flex: 1;
  margin-top: 0;
  padding: 0;
}

/* 登录提示区样式 */
.login-tip {
  background-color: #fff;
  padding: 15px 20px;
  margin-bottom: 20px;
  border-radius: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tip-text {
  color: #666;
  font-size: 14px;
}

.login-btn {
  font-size: 14px;
  padding: 8px 24px;
}

/* 保持其他样式不变 */
.news-item {
  margin-bottom: 20px;
}

.news-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
}

.news-text {
  flex: 1;
}

.news-text h3 {
  margin: 0 0 10px 0;
  font-size: 18px;
  line-height: 1.5;
}

.news-info {
  color: #999;
  font-size: 14px;
}

.news-info span {
  margin-right: 15px;
}

.news-image {
  width: 160px;
  height: 120px;
  border-radius: 4px;
}

.hot-list {
  position: sticky;
  top: 100px;
}

.hot-list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hot-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  cursor: pointer;
}

.hot-rank {
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  margin-right: 12px;
  font-weight: bold;
  color: #999;
}

.hot-rank.top3 {
  color: #f26d5f;
}

.hot-title {
  flex: 1;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hot-item:hover .hot-title {
  color: #406eeb;
}
</style>
